package com.device.operating.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;

import com.device.operating.entity.User;

public class Mainfilter  extends OncePerRequestFilter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

//	@Override
//	public void doFilterInternal(ServletRequest srq, ServletResponse srs, FilterChain fc)
//			throws IOException, ServletException {
//		// TODO Auto-generated method stub
//		HttpServletRequest request=(HttpServletRequest)srq;
//		HttpServletResponse response=(HttpServletResponse)srs;
//		HttpSession sesssion=request.getSession();
//		User user=sesssion.getAttribute("user")==null?null:(User)sesssion.getAttribute("user");
//		if(user==null){
//			response.sendRedirect("xasj/login");
//			return;
//		}
////		System.out.println("OKKKK");
//		String requrl=request.getRequestURI().replace("/xasj/", "");
//		System.out.println(requrl);
//		fc.doFilter(srq, srs);
//	}

	@Override
	protected void doFilterInternal(HttpServletRequest srq, HttpServletResponse srs, FilterChain fch)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpServletRequest request=(HttpServletRequest)srq;
		HttpServletResponse response=(HttpServletResponse)srs;
		HttpSession sesssion=request.getSession();		
		String requrl=request.getRequestURI().replace("/xasj/", "");
		if(requrl.contains(".js")||requrl.contains(".css")||requrl.contains(".jpg")||requrl.contains(".gif")||requrl.contains(".png")){
			fch.doFilter(srq, srs);
			return;
		}
		fch.doFilter(srq, srs);
		return;
		
//		User user=sesssion.getAttribute("user")==null?null:(User)sesssion.getAttribute("user");
//		if(user==null){
//			if(requrl.contains("login")){
//				fch.doFilter(srq, srs);
//				return;
//			}
//			response.sendRedirect("login");
//			return;
//		}
////		System.out.println("OKKKK");
//		String comdate=(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()); 
//		System.out.println(requrl+" "+comdate);
//		fch.doFilter(srq, srs);
	}

}
